package logic

import (
	"context"
	"good_rpc/models"

	"good_rpc/internal/svc"
	"good_rpc/pb/good"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGoodLogic {
	return &UpdateGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateGoodLogic) UpdateGood(in *good.GoodInfo) (*good.GoodInfo, error) {
	err := l.svcCtx.GoodModel.Update(l.ctx, &models.Good{Id: in.Id, Name: in.Name, Stock: in.Stock, Price: in.Price})
	if err != nil {
		l.Logger.Errorf("UpdateGood failed: id=%d, name=%s, stock=%d, price=%d, error: %v", in.Id, in.Name, in.Stock, in.Price, err)
		return nil, err
	}
	return &good.GoodInfo{Id: in.Id, Name: in.Name, Stock: in.Stock, Price: in.Price}, nil
}
